package service

import (
	"exam12_23/config"
	"exam12_23/internal/models/dao"
)

func CreateChat(message *dao.Message) bool {
	err := config.DB.Model(dao.Message{}).Create(message).Error
	if err != nil {
		return false
	}
	return true
}

func FindFriend(userId int, friendId int) string {
	var friends *dao.Friend
	config.DB.Debug().Model(&dao.Friend{}).Where("user_id=? and friend_id=?", friendId, userId).Limit(1).Find(&friends)
	if friends.ID == 0 {
		return "暂未添加好友"
	}
	return ""
}

func ListMessage(userId int, friendId int) []*dao.Message {
	var lm []*dao.Message
	var query = config.DB
	query = query.Model(&dao.Message{}).Where("send_id=? or read_id=? and send_id=? or read_id=? ", userId, userId, friendId, friendId).Find(&lm)
	return lm
}

func UpdateMessage(userId int, friendId int) {
	config.DB.Model(&dao.Message{}).Where("send_id=? and read_id=?", friendId, userId).Update("type", 2)
}
